package Leetcode;

import java.util.HashSet;
import java.util.LinkedList;
import java.util.Set;

/**
 * @ Author     ：侯耀祖
 * @ Description：
 */
public class searchRange {
    public int[] searchRange(int[] nums, int target) {
        int left = search(nums, target, true);
        if (left == -1){
            return new int[]{-1, -1};
        }
        int right = search(nums, target, false);
        return new int[]{left, right};
    }
    public int search(int[] nums, int target, boolean isFirst){
        int low = 0, high = nums.length - 1;
        int index = -1;
        while (low <= high){
            int mid = (high - low) / 2 + low;
            int num = nums[mid];
            if (num == target){
                index = mid;
                if (isFirst){
                    high = mid - 1;
                } else {
                    low = mid + 1;
                }
            } else if (num > target) {
                high = mid - 1;
            } else {
                low = mid + 1;
            }
        }
        return index;
    }
}
